package com.goparty.data.dao;

import java.util.List;

import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.goparty.data.model.Moment;
import com.goparty.data.model.MomentComment;
import com.goparty.data.model.UnReadQueue;
import com.goparty.data.model.UnReadQueuePK;

@Repository("unReadQueueDao")
@Transactional
public class UnReadQueueDao {
	private static Log log = LogFactory.getLog(UnReadQueueDao.class);
	
	@PersistenceContext
	private EntityManager em;
	
	public List<Moment> getUnReadMoment(String eventId, String receiverId, int offset, int limit){
		String sql = "select m.* from gp_moment m inner join gp_unread_queue q on m.id=q.recordId where q.receiverId=:receiverId and q.recordType='moment' and m.eventId=:eventId order by q.updateTime limit :limit offset :offset";
		
		Query q = em.createNativeQuery(sql, Moment.class);
		q.setParameter("eventId", eventId);
		q.setParameter("receiverId", receiverId);
		q.setParameter("limit", limit);
		q.setParameter("offset", offset);
		
		List<Moment> result = q.getResultList();
		
		String removeReadSql = "delete from gp_unread_queue where recordType=:recordType and recordId=:recordId and receiverId=:receiverId";
		Query removeReadQuery = em.createNativeQuery(removeReadSql);
		for(Moment m:result){
			removeReadQuery.setParameter("recordType", "moment");
			removeReadQuery.setParameter("recordId", m.getId());
			removeReadQuery.setParameter("receiverId", receiverId);
			removeReadQuery.executeUpdate();
		}
		
		return result;
	}
	
	public void insertUnReadMoment(String momentId, String publisherId){
		String sql = "insert gp_unread_queue(recordType,recordId,receiverId) select 'moment', :momentId, f.friendId from gp_user_friend f where f.userId=:publisherId";
		
		Query q = em.createNativeQuery(sql);
		q.setParameter("momentId", momentId);
		q.setParameter("publisherId", publisherId);
		
		q.executeUpdate();
		
	}
	
	public void insertUnReadMomentComment(String momentCommentId, String publisherId){
		String sql = "insert gp_unread_queue(recordType,recordId,receiverId) select 'moment_comment', :momentCommentId, f.friendId from gp_user_friend f where f.userId=:publisherId";
		
		Query q = em.createNativeQuery(sql);
		q.setParameter("momentCommentId", momentCommentId);
		q.setParameter("publisherId", publisherId);
		
		q.executeUpdate();
	}
	
	
	public List<MomentComment> getUnReadComments(String momentId, String receiverId, int offset, int limit){
		String sql = "select m.* from gp_moment_comment m inner join gp_unread_queue q on m.id=q.recordId where q.receiverId=:receiverId and q.recordType='moment_comment' and m.momentId=:momentId order by q.updateTime limit :limit offset :offset";
		
		Query q = em.createNativeQuery(sql, MomentComment.class);
		q.setParameter("momentId", momentId);
		q.setParameter("receiverId", receiverId);
		q.setParameter("limit", limit);
		q.setParameter("offset", offset);
		
		List<MomentComment> result = q.getResultList();
		
		String removeReadSql = "delete from gp_unread_queue where recordType=:recordType and recordId=:recordId and receiverId=:receiverId";
		Query removeReadQuery = em.createNativeQuery(removeReadSql);
		for(MomentComment m:result){
			removeReadQuery.setParameter("recordType", "moment_comment");
			removeReadQuery.setParameter("recordId", m.getId());
			removeReadQuery.setParameter("receiverId", receiverId);
			removeReadQuery.executeUpdate();
		}
		
		return result;
	}
	
	
}
